package redis

import (
	"encoding/json"
	"fmt"
	"github.com/go-redis/redis"
	"github.com/gogf/gf/os/glog"
	"micro-server/conf"
	"strings"
	"time"
)

var ClientRedis *redis.Client

func InitRedis() {
	proxyList := conf.RedisConf.ProxyList
	if len(proxyList) == 0 {
		return
	}
	proxy := strings.Split(proxyList[0], ":")
	options := redis.Options{
		Network:            "tcp",
		Addr:               fmt.Sprintf("%s:%s", proxy[0], proxy[1]),
		Dialer:             nil,
		OnConnect:          nil,
		Password:           conf.RedisConf.Password,
		DB:                 conf.RedisConf.DB,
		MaxRetries:         0,
		MinRetryBackoff:    0,
		MaxRetryBackoff:    0,
		DialTimeout:        0,
		ReadTimeout:        0,
		WriteTimeout:       0,
		PoolSize:           0,
		MinIdleConns:       0,
		MaxConnAge:         0,
		PoolTimeout:        0,
		IdleTimeout:        0,
		IdleCheckFrequency: 0,
		TLSConfig:          nil,
	}
	// 新建一个client
	ClientRedis = redis.NewClient(&options)
}

func Set(key string, value interface{}, expiration time.Duration) {
	ClientRedis.Set(key, value, expiration)
}

func Del(key string) {
	ClientRedis.Del(key)
}

func LLen(key string) (llen int64) {
	llen, err := ClientRedis.LLen(key).Result()
	if err != nil {
		glog.Error(fmt.Sprintf("err => %v", err))
		return
	}
	return
}

func Scan(key string) (scan []string) {
	scan, _, err := ClientRedis.Scan(uint64(0), key, 0).Result()
	if err != nil {
		glog.Error(fmt.Sprintf("err => %v", err))
	}
	return
}

func LPop(key string) (lpop string) {
	lpop, err := ClientRedis.LPop(key).Result()
	if err != nil {
		glog.Error(fmt.Sprintf("err => %v", err))
		return
	}
	return
}

func LSet(key string, index int64, value interface{}) {
	lset, err := ClientRedis.LSet(key, index, value).Result()
	if err != nil {
		glog.Error(fmt.Sprintf("err => %v; lset => %v", err, lset))
	}
}

func RPush(key string, values interface{}) {
	rpush, err := ClientRedis.RPush(key, values).Result()
	if err != nil {
		glog.Error(fmt.Sprintf("err => %v; rpush => %v", err, rpush))
	}
}

func LPush(key string, values interface{}) {
	lpush, err := ClientRedis.LPush(key, values).Result()
	if err != nil {
		glog.Error(fmt.Sprintf("err => %v; lpush => %v", err, lpush))
	}
}

func GetBool(key string) (b bool) {
	get, err := ClientRedis.Get(key).Result()
	if err == nil {
		b = true
	} else {
		b = false
	}
	fmt.Println(get)
	return
}

func GetInt(key string, defaultValue int) (result int) {
	get, err := ClientRedis.Get(key).Result()
	result = defaultValue
	if err == nil {
		err = json.Unmarshal([]byte(get), &result)
		if err != nil {
			glog.Error(fmt.Sprintf("err => %v", err))
		}
	} else {
		if err != redis.Nil {
			glog.Error(fmt.Sprintf("err => %v", err))
			return
		}
	}
	return
}

func GetString(key string) (result string, isNull bool) {
	get, err := ClientRedis.Get(key).Result()
	if err == nil {
		isNull = false
		err = json.Unmarshal([]byte(get), &result)
		if err != nil {
			glog.Error(fmt.Sprintf("err => %v", err))
		}
	} else {
		isNull = true
		if err != redis.Nil {
			glog.Error(fmt.Sprintf("err => %v", err))
			return
		}
	}
	return
}

func GetSlice(key string) (result []string, isNull bool) {
	get, err := ClientRedis.Get(key).Result()
	if err == nil {
		isNull = false
		err = json.Unmarshal([]byte(get), &result)
		if err != nil {
			glog.Error(fmt.Sprintf("err => %v", err))
		}
		return
	} else {
		isNull = true
		if err != redis.Nil {
			glog.Error(fmt.Sprintf("err => %v", err))
			return
		}
	}
	return
}